<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\service\weChat;

use EasyWeChat\Factory;
use think\facade\Config;

class WeChatAppletService{

    /*
    //小程序配置
    private $app_id = 'wx88c73f87a29b879b';                     //小程序appid
    private $secret_key = '82f34f17ec826942071b0e32a76cb76a';   //小程序秘钥
    //商户配置
    private $mch_id = '1526401891';                             //商户id
    private $mch_key = '717F00203CC111D45EAC5C3CA6612E26';      //小程序对应的微信商户秘钥
    private $cert_path = 'static\cert\apiclient_cert.pem';     //支付证书
    private $key_path = 'static\cert\apiclient_key.pem';       //支付秘钥证书
    */
    
     //小程序配置 婧羽新能源
    private $app_id = 'wxdd259141d3da4cd4';                     //小程序appid
    private $secret_key = '6deefc2563aaaee1acca673613976fb5';   //小程序秘钥
    //商户配置
    private $mch_id = '1648788556';                             //商户id
    private $mch_key = '8f608270f7f0461ee15f5fcabf8e1899';      //小程序对应的微信商户秘钥
    private $cert_path = 'static\cert\jingyu\apiclient_cert.pem';     //支付证书
    private $key_path = 'static\cert\jingyu\apiclient_key.pem';       //支付秘钥证书
    
    
    //获取基本的配置
    function getConfig(){
        return [
            'app_id' => $this->app_id,
            'secret' => $this->secret_key,
            'mch_id' => $this->mch_id,
        ];
    }

    /**
     * 获取支付配置
     * @return array
     */
    function mchConfig(){
        return [
            'mch_id' => $this->mch_id,
            'mch_key' => $this->mch_key,
            'cert_path' => $this->cert_path,
            'key_path' => $this->key_path,
        ];
    }

    /**
     * 小程序初始化
     * @return \EasyWeChat\MiniProgram\Application
     */
    function init(){
        $config = [
            'app_id' => $this->app_id,
            'secret' => $this->secret_key,
            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
//            'log' => [
//                'level' => 'debug',
//                'file' => __DIR__.'/wechat.log',
//            ],
        ];

        $weChatService = Factory::miniProgram($config);
        return $weChatService;
    }

    /**
     * 小程序支付类
     * @param $data
     * @return \EasyWeChat\Payment\Application
     */
    function payOrder($data=[]){
        $config = [
            'app_id' => $this->app_id,
            'key' => $this->mch_key,
            'mch_id' => $this->mch_id,
            'cert_path' => app()->getRootPath().$this->cert_path,
            'key_path' => app()->getRootPath().$this->key_path,
        ];
        if(isset($data['un_cert']) && $data['un_cert']){
            unset($config['cert_path']);
            unset($config['key_path']);
        }
        if(isset($data['notify_url']) && $data['notify_url']) $config['notify_url'] = $data['notify_url'];
        $weChatService = Factory::payment($config);
        return $weChatService;
    }



}
